package copyengine.utils
{
	import copyengine.ui.component.button.CEButton;
	import copyengine.ui.component.button.CESelectableButton;
	import copyengine.ui.component.button.animation.ICEButtonAnimation;
	import copyengine.ui.component.button.animation.ICESelectedButtonAnimation;
	import copyengine.ui.component.list.old.CEList;
	import copyengine.ui.component.list.old.CEListCore;
	import copyengine.ui.component.list.old.CEMultipleRowListCore;
	import copyengine.ui.component.list.old.CESingleRowListCore;
	import copyengine.ui.component.panel.CEScrollableVPanel;
	import copyengine.ui.component.panel.CEVPanel;
	import copyengine.ui.component.progressbar.CEProgressBar;
	import copyengine.ui.component.progressbar.animation.ICEProgressBarAnimation;
	import copyengine.ui.component.scrollbar.CEScrollBarCore;
	import copyengine.ui.component.tabbar.CETabBar;
	import copyengine.ui.component.tabbar.animation.ICETabBarAnimation;
	import copyengine.ui.component.tree.CETree;
	import copyengine.ui.font.FontEmbedManger;
	import copyengine.ui.text.CETextManger;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public final class UIUtils
	{
		public function UIUtils()
		{
		}

		/**
		 * Initialze for CEButton::
		 *
		 * CEButton have kinds of initialze way,
		 * -->1 Component both contain [label] and [bg] attribute
		 * 				those btn is LabelBtn ,and the TextFiled will auto font Embed by FontEmbedManger
		 *
		 * -->2 Componet contain nothing
		 * 				those btn is IconBtn , and it will use the component itself as the [bg] attribtue
		 *
		 * ===========================================================
		 * Animation for CEButton::
		 *
		 * CEButtonTweenAnimation::
		 *
		 * 		this need componet ONLY contain one frame , and it will scale the component size by TweenMax
		 *
		 * CEButtomScaleAnimation::
		 *
		 * 		this need componet ONLY contain one frame , and it will scale the component without tween.
		 *
		 * CEButtonFrameAnimation::
		 *
		 * 		this need component contain three frame, and the frame MUST contain frame lable
		 * "on","down","over"
		 *
		 * ==========================================================
		 * Optional Property for CEButton::
		 *
		 * textKey ::
		 *
		 * 		this property link to i18n system , use this key can find find the text by CETextManger,and
		 * auto embed the text to current Button
		 *
		 */
		public static function getButton(_btnSymbol:MovieClip,
										 _animationClass:Class=null,
										 _textKey:String=null,
										 _animationArg:Object=null):CEButton
		{
			var btn:CEButton=new CEButton(_btnSymbol, _textKey);
			var animation:ICEButtonAnimation;
			if (_animationClass != null)
			{
				animation=new _animationClass(_animationArg);
				animation.setTarget(_btnSymbol);
			}
			btn.setAnimation(animation);

			return btn;
		}

		/**
		 * Initialze for CESelectableButton::
		 *
		 * 		CESelectableBtn is same as CEButton for init stuff
		 * ========================================
		 * Animation::
		 *
		 * CESelectedButtonTweenAnimation::
		 * CESelectedButtonScaleAnimation::
		 *
		 * 		this need componet only contain TWO frame , the first frame is the componet not be selected
		 * and the second frame is the component been selected.
		 * and the animation part is same as CEButtonAnimation
		 * =========================================
		 *  Optional Property for CESelectableButton::
		 *
		 * isClickToSelected::
		 *
		 * 		this property is define is current Button will change between select state and unSelect state by user mouse click.
		 * if not , then need to call button.selected= true||false to change it.
		 *
		 */
		public static function getSelectableButton(_btnSymbol:MovieClip,
												   _animationClass:Class,
												   _textKey:String=null,
												   _isClickToSelected:Boolean=true):CESelectableButton
		{
			var btn:CESelectableButton=new CESelectableButton(_btnSymbol, _textKey, _isClickToSelected);
			var animation:ICESelectedButtonAnimation;
			if (_animationClass != null)
			{
				animation=new _animationClass();
				animation.setTarget(_btnSymbol);
			}
			btn.setAnimation(animation);

			return btn;
		}


		public static function getScrollBar(_scrollBarSymbol:MovieClip, _isAutoScaleScrollBarThumb:Boolean=true, _thumbAnimationClass:Class=null,
											_trackAnimationClass:Class=null):CEScrollBarCore
		{
			var thumb:CEButton=getButton(_scrollBarSymbol["thumb"], _thumbAnimationClass, null);
			var track:CEButton=getButton(_scrollBarSymbol["track"], _trackAnimationClass, null);
			var scrollBar:CEScrollBarCore=new CEScrollBarCore(_scrollBarSymbol, thumb, track, CEScrollBarCore.LAYOUT_AUTO, _isAutoScaleScrollBarThumb);

			return scrollBar;
		}

		public static function getProgressBar(_progressBarSymbol:MovieClip, _animationClass:Class=null):CEProgressBar
		{
			var progressBar:CEProgressBar=new CEProgressBar(_progressBarSymbol);
			var animation:ICEProgressBarAnimation;
			if (_animationClass != null)
			{
				animation=new _animationClass();
				animation.setTarget(_progressBarSymbol);
			}

			progressBar.setAnimation(animation);

//			_progressBarSymbol.addChild(progressBar);

			return progressBar;
		}

		/**
		 * Initialze ::
		 *
		 * 		all button for tabbar muse be SELECTABLE button
		 * ========================================
		 * Optional Property for CETabBar::
		 *
		 * btnNameArray::
		 *
		 * 			function will use tabBarSymbol[btnName[*]] to location the btn symbol
		 *
		 * btnAnimation::
		 *
		 * 			denfine each btn animation class , for now we assume all btn will use the same type animation class,but maybe change later
		 *
		 */
		public static function getTabBar(_tabBarSymbol:MovieClip,
										 _btnNameArray:Array,
										 _btnAnimation:Class=null,
										 _tabAnimation:ICETabBarAnimation=null,
										 _isClickToSelected:Boolean=false,
										 _btnLableKeyArray:Array=null):CETabBar
		{
//			var tabBtns:Vector.<CESelectableButton> = new Vector.<CESelectableButton>();
//			for (var i:int = 0; i < _btnNameArray.length; i++)
//			{
//				var lableKey:String;
//				if (_btnLableKeyArray != null && _btnLableKeyArray[i] != null)
//				{
//					lableKey = _btnLableKeyArray[i];
//				}
//				var btnName:String = _btnNameArray[i];
//				var btn:CESelectableButton = getSelectableButton(_tabBarSymbol[btnName], _btnAnimation, lableKey, _isClickToSelected);
//				tabBtns.push(btn);
//			}
//
//			var tabBar:CETabBar = new CETabBar(_tabBarSymbol, tabBtns);
//			if (_tabAnimation != null)
//			{
//				_tabAnimation.setTarget(tabBar, _tabBarSymbol);
//				tabBar.setAnimation(_tabAnimation);
//			}
//
////			_tabBarSymbol.addChild(tabBar);
//
//			return tabBar;
			return null;
		}

		public static function getCEVPanel(_vPanelSymbol:MovieClip, _buttonAnimation:Class=null,
										   _scrollBarThumBtnAnimation:Class=null,
										   _scrollBarTrackBtnAnimation:Class=null,
										   _isAutoScaleScrollBarThumb:Boolean=false,
										   _vGap:int=0):CEVPanel
		{
			var scrollBarSymbol:MovieClip=_vPanelSymbol["scrollBar"];
			var scrollBar:CEScrollBarCore
			if (scrollBarSymbol != null)
			{
				scrollBar=getScrollBar(scrollBarSymbol, _isAutoScaleScrollBarThumb, _scrollBarThumBtnAnimation, _scrollBarTrackBtnAnimation);
			}

			var nextOneBtnSymbol:MovieClip=_vPanelSymbol["nextOne"];
			var nextOneBtn:CEButton;
			if (nextOneBtnSymbol != null)
			{
				nextOneBtn=getButton(nextOneBtnSymbol, _buttonAnimation, null);
			}

			var prevOneBtnSymbol:MovieClip=_vPanelSymbol["prevOne"];
			var prevOneBtn:CEButton;
			if (prevOneBtnSymbol != null)
			{
				prevOneBtn=getButton(prevOneBtnSymbol, _buttonAnimation, null);
			}

			var nextPageBtnSymbol:MovieClip=_vPanelSymbol["nextPage"];
			var nextPageBtn:CEButton;
			if (nextPageBtnSymbol != null)
			{
				nextPageBtn=getButton(nextPageBtnSymbol, _buttonAnimation, null);
			}

			var prevPageBtnSymbol:MovieClip=_vPanelSymbol["prevPage"];
			var prevPageBtn:CEButton;
			if (prevPageBtnSymbol != null)
			{
				prevPageBtn=getButton(prevPageBtnSymbol, _buttonAnimation, null);
			}

			var startBtnSymbol:MovieClip=_vPanelSymbol["start"];
			var startBtn:CEButton;
			if (startBtnSymbol != null)
			{
				startBtn=getButton(startBtnSymbol, _buttonAnimation, null);
			}

			var endBtnSymbol:MovieClip=_vPanelSymbol["end"];
			var endBtn:CEButton;
			if (endBtnSymbol != null)
			{
				endBtn=getButton(endBtnSymbol, _buttonAnimation, null);
			}

			//替换ScrollVPanel到PanelMc的位置
			var panelMc:MovieClip=_vPanelSymbol["panelMc"];
			var rePanelMc:Rectangle=panelMc.getRect(panelMc.parent);
			var ceScrollableVPanel:CEScrollableVPanel=new CEScrollableVPanel(panelMc.width, panelMc.height, _vGap);

			ceScrollableVPanel.x=rePanelMc.left;
			ceScrollableVPanel.y=rePanelMc.top;
			panelMc.parent.addChild(ceScrollableVPanel);
			panelMc.alpha=0;

			var ceVPanel:CEVPanel=new CEVPanel(_vPanelSymbol,
											   ceScrollableVPanel,
											   scrollBar,
											   nextOneBtn,
											   nextPageBtn,
											   prevOneBtn,
											   prevPageBtn,
											   startBtn,
											   endBtn);
			_vPanelSymbol.addChild(ceScrollableVPanel);
			return ceVPanel;
		}



		public static function getMultipleList(_listSymbol:MovieClip,
											   _layout:String,
											   _buttonAnimation:Class,
											   _scrollBarThumBtnAnimation:Class,
											   _scrollBarTrackBtnAnimation:Class,
											   _isAutoScaleScrollBarThumb:Boolean=true):CEList
		{
			var c0:MovieClip=_listSymbol["c0"];
			var c00:MovieClip=c0["c0"];
			var c01:MovieClip=c0["c1"];
			var c1:MovieClip=_listSymbol["c1"];

			var cellRenderWidth:Number=c00.width;
			var cellRenderHeight:Number=c00.height;
			var listCorePosX:Number=c0.x;
			var listCorePosY:Number=c0.y;

			var contentPadding:Number;
			if (_layout == CEListCore.LAYOUT_HORIZONTAL)
			{
				if (c1)
				{
					contentPadding=c1.x - c0.x - c0.width;
				}
				else
				{
					//只有C0 无C1（只显示一列）
					contentPadding=10;
				}
			}
			else
			{
				if (c1)
				{
					contentPadding=c1.y - c0.y - c0.height;
				}
				else
				{
					contentPadding=10;
				}
			}

			var groupCellRenderPadding:Number
			//groupCellRenderPadding is opposite with layout
			if (_layout == CEListCore.LAYOUT_HORIZONTAL)
			{
				groupCellRenderPadding=c01.y - c00.y - c00.height;
			}
			else
			{
				groupCellRenderPadding=c01.x - c00.x - c00.width;
			}

			var eachGroupDisplayCount:int=0;
			while (c0["c" + eachGroupDisplayCount] != null)
			{
				eachGroupDisplayCount++;
			}

			var displayCount:int=0;
			while (_listSymbol["c" + displayCount] != null)
			{
				for (var currentGroupCount:int=0; currentGroupCount < eachGroupDisplayCount; currentGroupCount++)
				{
					//如果以后需要把List放入元件的帧中，则不删除render元件，而是将其visable置成false
					GeneralUtils.removeTargetFromParent(_listSymbol["c" + displayCount]["c" + currentGroupCount]);
				}
				displayCount++;
			}

			var listCore:CEListCore=new CEMultipleRowListCore(displayCount, eachGroupDisplayCount, _layout, cellRenderWidth, cellRenderHeight, contentPadding, groupCellRenderPadding);
			listCore.x=listCorePosX;
			listCore.y=listCorePosY;

			var scrollBarSymbol:MovieClip=_listSymbol["scrollBar"];
			var scrollBar:CEScrollBarCore
			if (scrollBarSymbol != null)
			{
				scrollBar=getScrollBar(scrollBarSymbol, _isAutoScaleScrollBarThumb, _scrollBarThumBtnAnimation, _scrollBarTrackBtnAnimation);
			}

			var nextOneBtnSymbol:MovieClip=_listSymbol["nextOne"];
			var nextOneBtn:CEButton;
			if (nextOneBtnSymbol != null)
			{
				nextOneBtn=getButton(nextOneBtnSymbol, _buttonAnimation, null);
			}

			var prevOneBtnSymbol:MovieClip=_listSymbol["prevOne"];
			var prevOneBtn:CEButton;
			if (prevOneBtnSymbol != null)
			{
				prevOneBtn=getButton(prevOneBtnSymbol, _buttonAnimation, null);
			}

			var nextPageBtnSymbol:MovieClip=_listSymbol["nextPage"];
			var nextPageBtn:CEButton;
			if (nextPageBtnSymbol != null)
			{
				nextPageBtn=getButton(nextPageBtnSymbol, _buttonAnimation, null);
			}

			var prevPageBtnSymbol:MovieClip=_listSymbol["prevPage"];
			var prevPageBtn:CEButton;
			if (prevPageBtnSymbol != null)
			{
				prevPageBtn=getButton(prevPageBtnSymbol, _buttonAnimation, null);
			}

			var startBtnSymbol:MovieClip=_listSymbol["start"];
			var startBtn:CEButton;
			if (startBtnSymbol != null)
			{
				startBtn=getButton(startBtnSymbol, _buttonAnimation, null);
			}

			var endBtnSymbol:MovieClip=_listSymbol["end"];
			var endBtn:CEButton;
			if (endBtnSymbol != null)
			{
				endBtn=getButton(endBtnSymbol, _buttonAnimation, null);
			}

			var list:CEList=new CEList(_listSymbol, listCore, scrollBar, nextOneBtn, nextPageBtn, prevOneBtn, prevPageBtn, startBtn, endBtn);

			_listSymbol.addChild(list);

			return list;
		}

		/**
		 * use an movieClipe to create an list. the movieClipe should
		 * 1` at last have two cellRender , and the cellRender should name as c0,c1,c2....
		 * 		this function will use c0,c1 to caulate,and use the max cellRender number ex: c5 to caulate the display count.
		 * 2`it can include(not require) btns named "nextOne" , "prevOne" , "nextPage" , "prevPage" ,"start" , "end"
		 * 3`it can include(not require) an scrollBar named "scrollBar".
		 */
		public static function getList(_listSymbol:MovieClip,
									   _layout:String,
									   _buttonAnimation:Class,
									   _scrollBarThumBtnAnimation:Class,
									   _scrollBarTrackBtnAnimation:Class,
									   _isAutoScaleScrollBarThumb:Boolean=true):CEList
		{
			var c0:MovieClip=_listSymbol["c0"];
			var c1:MovieClip=_listSymbol["c1"];
			//WARNING::
			//cellRenderSymbol should use center author 
			var listCorePosX:Number=c0.x - (c0.width >> 1);
			var listCorePosY:Number=c0.y - (c0.height >> 1);
			var cellRenderWidth:Number=c0.width;
			var cellRenderHeight:Number=c0.height;
			var contentPadding:Number;
			if (_layout == CEListCore.LAYOUT_HORIZONTAL)
			{
				//没有C1则只显示一个
				if (c1)
				{
					contentPadding=c1.x - c0.x - c0.width;
				}
				else
				{
					contentPadding=10;
				}
			}
			else
			{
				if (c1)
				{
					contentPadding=c1.y - c0.y - c0.height;
				}
				else
				{
					contentPadding=10;
				}
			}
			var displayCount:int=0;
			while (_listSymbol["c" + displayCount] != null)
			{
				//如果以后需要把List放入元件的帧中，则不删除render元件，而是将其visable置成false
				GeneralUtils.removeTargetFromParent(_listSymbol["c" + displayCount]);
				displayCount++;
			}
//			var listCore:CEListCore = new CEListCore(displayCount, _layout, cellRenderWidth, cellRenderHeight, contentPadding);
			var listCore:CEListCore=new CESingleRowListCore(displayCount, _layout, cellRenderWidth, cellRenderHeight, contentPadding);
			listCore.x=listCorePosX;
			listCore.y=listCorePosY;

			var scrollBarSymbol:MovieClip=_listSymbol["scrollBar"];
			var scrollBar:CEScrollBarCore
			if (scrollBarSymbol != null)
			{
				scrollBar=getScrollBar(scrollBarSymbol, _isAutoScaleScrollBarThumb, _scrollBarThumBtnAnimation, _scrollBarTrackBtnAnimation);
			}

			var nextOneBtnSymbol:MovieClip=_listSymbol["nextOne"];
			var nextOneBtn:CEButton;
			if (nextOneBtnSymbol != null)
			{
				nextOneBtn=getButton(nextOneBtnSymbol, _buttonAnimation, null);
			}

			var prevOneBtnSymbol:MovieClip=_listSymbol["prevOne"];
			var prevOneBtn:CEButton;
			if (prevOneBtnSymbol != null)
			{
				prevOneBtn=getButton(prevOneBtnSymbol, _buttonAnimation, null);
			}

			var nextPageBtnSymbol:MovieClip=_listSymbol["nextPage"];
			var nextPageBtn:CEButton;
			if (nextPageBtnSymbol != null)
			{
				nextPageBtn=getButton(nextPageBtnSymbol, _buttonAnimation, null);
			}

			var prevPageBtnSymbol:MovieClip=_listSymbol["prevPage"];
			var prevPageBtn:CEButton;
			if (prevPageBtnSymbol != null)
			{
				prevPageBtn=getButton(prevPageBtnSymbol, _buttonAnimation, null);
			}

			var startBtnSymbol:MovieClip=_listSymbol["start"];
			var startBtn:CEButton;
			if (startBtnSymbol != null)
			{
				startBtn=getButton(startBtnSymbol, _buttonAnimation, null);
			}

			var endBtnSymbol:MovieClip=_listSymbol["end"];
			var endBtn:CEButton;
			if (endBtnSymbol != null)
			{
				endBtn=getButton(endBtnSymbol, _buttonAnimation, null);
			}

			var list:CEList=new CEList(_listSymbol, listCore, scrollBar, nextOneBtn, nextPageBtn, prevOneBtn, prevPageBtn, startBtn, endBtn);

			_listSymbol.addChild(list);

			return list;
		}


		public static function getTree(_mc:MovieClip,
									   _layout:String,
									   _buttonAnimation:Class,
									   _scrollBarThumBtnAnimation:Class,
									   _scrollBarTrackBtnAnimation:Class,
									   _isAutoScaleScrollBarThumb:Boolean=true,
									   _isAutoUnfold:Boolean=true):CETree
		{
			var treeComponent:CETree=new CETree(_mc,
												_layout,
												_buttonAnimation,
												_scrollBarThumBtnAnimation,
												_scrollBarTrackBtnAnimation,
												_isAutoScaleScrollBarThumb,
												_isAutoUnfold);
			return treeComponent;
		}

		public static function embedTF(_mc:MovieClip, _tfKey:String, _textKey:String=null):TextField
		{
			var tf:TextField=_mc[_tfKey];
			if (tf)
			{
				FontEmbedManger.instance.embedTextField(tf);
				if (_textKey)
				{
					tf.text=CETextManger.instance.getText(_textKey);
				}
			}
			return tf;
		}

		/**
		 * replace target and source use the same anchor
		 * 			_target.x = _source.x;
		   _target.y = _source.y;
		 */
		public static const ADJUST_POS_ORIGIN:int=0;

		/**
		 * replace target use source width/height as the warp bounds.
		 * set it to center of the target( no matter _target anchor at the center or top-left)
		 */
		public static const ADJUST_POS_CENTER_FILL:int=1;

		/**
		 * no change the target size
		 */
		public static const ADJUST_SIZE_NONE:int=0;

		/**
		 *change target width/height to source's widht/height
		 * 			_target.width = _source.width;
		   _target.height = _source.height;
		 */
		public static const ADJUST_SIZE_ALL:int=1;

		/**
		 * change target size just can fill into the source bounds, use the min scaleX/scaleY
		 * will not change aspect ratio.
		 */
		public static const ADJUST_SIZE_SCALE:int=2;


		public static function replaceComponent(_target:DisplayObject, _placeHolder:DisplayObjectContainer,
												_replaceSize:int=ADJUST_SIZE_SCALE,
												_replacePos:int=ADJUST_POS_CENTER_FILL,
												_placeHolderAlpha:Number=0):void
		{
			//防止Target在传入时已经缩放
			_target.scaleX=_target.scaleY=1;
			if (_placeHolder != null && _placeHolder.parent != null && _target != null)
			{
				//1`	change target size
				if (_replaceSize == ADJUST_SIZE_ALL)
				{
					_target.width=_placeHolder.width;
					_target.height=_placeHolder.height;
				}
				else if (_replaceSize == ADJUST_SIZE_SCALE)
				{
					var scale:Number=Math.min(_placeHolder.width / _target.width, _placeHolder.height / _target.height);
					_target.scaleX=_target.scaleY=scale;
				}

				//2` 	move target to position
				_placeHolder.parent.addChildAt(_target, _placeHolder.parent.getChildIndex(_placeHolder) + 1);
				//Mc的坐标点就是Ph的坐标点
				if (_replacePos == ADJUST_POS_ORIGIN)
				{
					_target.x=_placeHolder.x;
					_target.y=_placeHolder.y;
				}
				//Mc被居中放在Ph里面
				else if (_replacePos == ADJUST_POS_CENTER_FILL)
				{
					var rTarget:Rectangle=_target.getBounds(_target);
					var rPh:Rectangle=_placeHolder.getBounds(_placeHolder.parent);
					_target.x=rPh.x + rPh.width / 2 - rTarget.width * _target.scaleX / 2 - rTarget.x * _target.scaleX;
					_target.y=rPh.y + rPh.height / 2 - rTarget.height * _target.scaleY / 2 - rTarget.y * _target.scaleY;
				}

				//3`将Ph的alpha设置成0,但不删除元件,因为删除元件会导致MovieClip重新new出新的ph
				_placeHolder.alpha=_placeHolderAlpha;
			}
		}

		public static function adjustPos(_target:DisplayObject, _refer:DisplayObject, _type:int):void
		{
			if (_refer != null && _refer.parent != null && _target != null)
			{
				if (_type == ADJUST_POS_ORIGIN)
				{
					_target.x=_refer.x;
					_target.y=_refer.y;
				}
				else if (_type == ADJUST_POS_CENTER_FILL)
				{
					var rTarget:Rectangle=_target.getBounds(_target.parent);
					var rRefer:Rectangle=_refer.getBounds(_refer.parent);
					_target.x+=rRefer.x + rRefer.width / 2 - rTarget.width / 2 - rTarget.x;
					_target.y+=rRefer.y + rRefer.height / 2 - rTarget.height / 2 - rTarget.y;
				}
			}
		}

	}
}
